package com.fengmi;

import freemarker.template.Configuration;
import freemarker.template.Template;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：铁蛋
 * 时间：2021/10/21 10:56
 * 描述：永无Bug
 */
public class FreemarkerDemo {
    public static void main(String[] args) throws Exception {
//        1.构建Configuration对象
        Configuration configuration=new Configuration(Configuration.getVersion());
//        2.设置模板文件目录(Directory)
            configuration.setDirectoryForTemplateLoading(new File("D:\\JAVA\\idea\\锋迷铺子门户系统\\后台\\fengmi-mall\\fengmi-html\\src\\main\\resources\\freemarker"));
//        3.设置字符集
        configuration.setDefaultEncoding("utf-8");
//        4.获取模板对象
        Template template = configuration.getTemplate("demo.ftl");
//        5.创建模型数据
        List<Student> students=new ArrayList<Student>(){{
           add(new Student("jack",18));
           add(new Student("rose",20));
        }};
        Map data =new HashMap(){{
            put("username","张三");
            put("students",students);
        }};

//        6.创建输出流(FileWriter)对象
        FileWriter fileWriter = new FileWriter("D:\\JAVA\\idea\\demo.html");
//        7.输出
        template.process(data,fileWriter);
//        8.关闭
        fileWriter.close();
    }
}
